﻿using System;
using System.Configuration;
using System.IO;
using System.Reflection;

namespace EntLibSample.DALCommon
{
    public class SysLog
    {
        private static SysLog sysLog = null;
        
        public enum EnumMethod
        {
            Console,
            File,
            WindowsLog
        }

        public string FileName { get; set; }

        private EnumMethod LogMethod = EnumMethod.File;

        private SysLog()
        {
            FetchLogMethod();
            FetchLogFileName();
        }

        public static SysLog GetInstance()
        {
            if (sysLog == null)
            {
                sysLog = new SysLog();
            }
            return sysLog;
        }

        private void FetchLogMethod()
        {
            string strLogMethod = ConfigurationManager.AppSettings["LogMethod"];
            switch (strLogMethod)
            {
                case "File":
                    LogMethod = EnumMethod.File;
                    break;
                case "Console":
                    LogMethod = EnumMethod.Console;
                    break;
                case "WindowsLog":
                    break;
                default:
                    LogMethod = EnumMethod.Console;
                    break;
            }
        }

        private void FetchLogFileName()
        {
            FileName = ConfigurationManager.AppSettings["LogFileName"];
            string strPath = Path.GetDirectoryName(FileName);
            string strTheFileName = Path.GetFileName(FileName);
            if (strPath == null || strPath.Trim().Length == 0)
            {
                strPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                FileName = strPath + "\\" + strTheFileName;
            }
            if (FileName == null || FileName.Trim().Length == 0)
            {
                FileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\App.log";
            }
        }

        public void LogMessage(string strMessage)
        {
            switch (LogMethod)
            {
                case EnumMethod.File:
                    FileStream file = new FileStream(FileName, FileMode.Append);
                    StreamWriter writer = new StreamWriter(file);
                    writer.WriteLine(DateTime.Now.ToString() + " " + strMessage);
                    writer.Close();
                    file.Close();
                    break;
                case EnumMethod.Console:
                    Console.WriteLine(DateTime.Now.ToString() + " " + strMessage);
                    break;
                case EnumMethod.WindowsLog:
                    //Not implemented yet.
                    break;
                default:
                    break;
            }
        }

        public void LogError(System.Exception ex)
        {
            LogMessage(ex.Message);
            LogMessage(ex.StackTrace);
        }
    }
}
